package com.huabo.fxgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.util.JsonBean;
import com.huabo.fxgl.entity.*;
import com.huabo.fxgl.service.*;
import com.huabo.fxgl.service.impl.YyCompanyServiceImpl;
import com.netflix.discovery.converters.Auto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.spring.web.json.Json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  对应风险监控子模块 - 供应商监控页面
 * </p>
 * @version 1.0.1
 * @author LiYe
 * @since 2022-08-02
 */
@RestController
@RequestMapping("/fxxt")
@Api(tags = "风险管控 - 风险监控 - 供应商监控")
@Slf4j
public class YyCompanyController {
    @Autowired
    private IYyCompanyService yyCompanyService;
    @Autowired
    private IYyPriceService yyPriceService;
    @Autowired
    private IStaffService staffService;
    @Autowired
    private IBiPageService biPageService;
    @Autowired
    private IYyTeamService yyTeamService;
    /**
     * 风险管控 - 风险监控 - 供应商监控 - 供应商列表
     * @author LiYe
     * @version 1.0.1
     * @date 2022/8/2
     * @param find
     * @return
     */
    @RequestMapping(value = "/fxyj/fxyj_list")
//    @ApiOperation("/fxxt/fxyj/fxyj_list")
    @ApiOperation(value = "供应商列表 /fxxt/fxyj/fxyj_list")
    public JsonBean fxyj_fxyj_list(@ApiParam(name = "find", value = "find") Find find,
                                   @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(defaultValue = "1") Integer pageNumber,
                                   @ApiParam(name = "limit", value = "limit") @RequestParam(defaultValue = "15") Integer limit,
                                   @ApiParam(name = "teamid", value = "teamid") @RequestParam(required = false) Integer teamid,
                                   @ApiParam(name = "staff", value = "staff") Staff staff,
                                   @ApiParam(name = "organization", value = "organization") Organization organization) {
        IPage page = new Page(pageNumber, limit);//分页设置
        IPage pageBean = null;
//        log.info("organization: " + organization);
        log.info("teamid: " + teamid);
        if(teamid!=null) {
            pageBean = yyCompanyService.findCompanyListByTeamid(page, find, teamid);
        } else {
            pageBean = yyCompanyService.findCompanyList(page, find, organization.getOrgid().intValue(), staff.getStaffid().intValue());
        }
        JsonBean jsonBean = new JsonBean();
        jsonBean.setCode(200);
        Map result = new HashMap();
        result.put("pageBean", pageBean);
        result.put("teamid", teamid);
        result.put("pageNumber", pageNumber);
        result.put("fxtype", find.getFxtype());
        jsonBean.setData(result);

        return jsonBean;
    }


    /**
     * 供应商监控主页的数据查找：监控内部数据、监控外部数据、分组数据等
     * @author liye
     * @Date 2022/8/11
     * @param staff 员工信息
     * @param organization 组织信息
     * @return
     */
    @RequestMapping(value = "/riskwarning/main")
    @ApiOperation(value = "主页数据 /fxxt/riskwarning/main")
    //测试数据：staffid=30, orgid=1
    public JsonBean main(@ApiParam(name = "staff", value = "staff") Staff staff,
                         @ApiParam(name = "organization", value = "organization") Organization organization) {

        Map result = new HashMap();
        //监控外部数据
        List<YyPrice> list = yyPriceService.getAll();
        if(list!=null && list.size()>0) {
            result.put("list", list);
        }
        staff = staffService.getById(staff.getStaffid());//使用staffid查找完整的staff数据

        //查询监控内部数据
        List<BiPage> pageList = biPageService.getByOrgidAndStaffid(staff.getOrgid(), staff.getStaffid());
        if(pageList!=null && pageList.size()>0) {
            result.put("pageChilds", pageList);
        }
        //查询分组列表
        List<YyTeam> teamList = yyTeamService.getByOrgidAndStaffid(organization.getOrgid(), staff.getStaffid());
        if(teamList!=null && teamList.size()>0) {
            result.put("teams", teamList);
            result.put("count", teamList.size());
        }

        JsonBean jsonBean = new JsonBean();
        jsonBean.setCode(200);
        jsonBean.setMsg("success");
        jsonBean.setData(result);
        return jsonBean;
    }
}
